package com.oracle.zebrafreelytraveling.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.oracle.zebrafreelytraveling.R;
import com.oracle.zebrafreelytraveling.data.RequestResult.ResBase;
import com.oracle.zebrafreelytraveling.data.RequestResult.ResCircle;
import com.oracle.zebrafreelytraveling.data.bean.User;
import com.oracle.zebrafreelytraveling.net.HttpManager;
import com.oracle.zebrafreelytraveling.ui.BaseRecycleAdapter.CommonAdapter;
import com.oracle.zebrafreelytraveling.ui.BaseRecycleAdapter.MultiItemTypeAdapter;
import com.oracle.zebrafreelytraveling.ui.BaseRecycleAdapter.base.ViewHolder;
import com.oracle.zebrafreelytraveling.ui.BaseRecycleAdapter.wrapper.LoadMoreWrapper;
import com.oracle.zebrafreelytraveling.ui.adapter.CircleListAdapter;
import com.oracle.zebrafreelytraveling.utils.StringUtil;
import com.oracle.zebrafreelytraveling.utils.ToastUtil;
import com.oracle.zebrafreelytraveling.utils.UserUtil;
import com.oracle.zebrafreelytraveling.utils.constant.Constant;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by yydcnjjw on 2017/8/10.
 */

public class MyCircleActivity extends BaseActitvity {
    private RecyclerView mCircleRecycle;
    private CommonAdapter<ResCircle.DataBean> mConcernAdapter;
    private List<ResCircle.DataBean> mCircleList;
    private LoadMoreWrapper mLoadMoreWrapper;

    private int page = 0;
    private boolean isListNull = false;
    private ViewHolder mLoadMoreHolder;
    private Disposable mDisposable;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initRecycleView();
    }

    private void initRecycleView(){
        mCircleRecycle = (RecyclerView) findViewById(R.id.circle_recycle_view);
        mCircleRecycle.setLayoutManager(new LinearLayoutManager(this));
        mCircleList = new ArrayList<>();
        mConcernAdapter = new CircleListAdapter(this, R.layout.item_chat_circle, mCircleList);
        mConcernAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                ResCircle.DataBean dataBean = mCircleList.get(position);
                CircleIntroActivity.startCircleIntroActivity(MyCircleActivity.this, dataBean);
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });
        mLoadMoreWrapper = new LoadMoreWrapper(mConcernAdapter);
        mLoadMoreWrapper.setLoadMoreView(R.layout.footer_loading);
        mLoadMoreWrapper.setOnLoadMoreListener(viewHolder -> {
            mLoadMoreHolder = viewHolder;
            if (!isListNull){
                loadCircleList();
            } else {
                viewHolder.getConvertView().setVisibility(View.GONE);
            }
        });
        mCircleRecycle.setAdapter(mLoadMoreWrapper);
    }

    private void loadCircleList(){
        User user = UserUtil.getUserInfoFromSP();
        if (user == null){
            return;
        }
        HttpManager.getInstance().getService(Constant.BASE_URL)
                .getCircleByUserId(user.getUserId(), page, Constant.MAX_PAGE)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ResCircle>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mDisposable = d;
                    }

                    @Override
                    public void onNext(@NonNull ResCircle resCircle) {
                        if (resCircle.getCode().equals(Constant.REQUEST_SUCCESS)){
                            mCircleList.addAll(resCircle.getData());
                            isListNull = false;
                        } else {
                            ToastUtil.showToast(getString(R.string.not_any_more));
                            isListNull = true;
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        Log.d("loadCircleList", e.getMessage());
                        mLoadMoreHolder.getConvertView().setVisibility(View.GONE);
                    }

                    @Override
                    public void onComplete() {
                        mLoadMoreHolder.getConvertView().setVisibility(View.GONE);
                        mLoadMoreWrapper.notifyDataSetChanged();
                    }
                });
        page++;
    }

    public static void startMyCircleActivity(Activity activity){
        activity.startActivity(new Intent(activity, MyCircleActivity.class));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_personal_circle, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.addCircle:
                //TODO 创建圈子
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //清除 Disposable
        if (mDisposable != null && !mDisposable.isDisposed()) {
            mDisposable.dispose();
        }
        page = 0;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_my_circle;
    }
}
